package ru.housewife.controllers.user;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import ru.common.hibernate.HibernateActionExecutor;
import ru.housewife.AppConstants;
import ru.housewife.cache.CacheManager;
import ru.housewife.controllers.AbstractController;
import ru.housewife.form.UserForm;
import ru.housewife.service.user.LogonUser;
import ru.housewife.service.user.RegisterNewUser;

import javax.servlet.http.HttpServletRequest;

import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

/**
 * users controller
 * handles:
 *      login
 *      logout
 *      registration
 *      users changes
 *
 * @author Alexander Troshanin
 */
@Controller
public class UserController extends AbstractController {

    private final HibernateActionExecutor hiber;

    public UserController(@Qualifier("houseWifesHibernate") HibernateActionExecutor hiber) {
        this.hiber = hiber;
    }

    @RequestMapping(value = "/register.html", method = GET)
    public ModelAndView getRegisterUserForm() {
        return new ModelAndView("housewifes/users/registerUserPage", "user", new UserForm());
    }

    @RequestMapping(value = "/register.html", method = POST)
    public ModelAndView submitRegistrationForm(@ModelAttribute("user") UserForm user, Errors errors) {
        user.validate(errors);
        if (errors.hasErrors()) {
            return new ModelAndView("housewifes/users/registerUserPage");
        }
        hiber.execute(new RegisterNewUser(user, errors));
        if (errors.hasErrors()) {
            return new ModelAndView("housewifes/users/registerUserPage");
        }
        return new ModelAndView("housewifes/users/registerUserFinishedPage");
    }

    @RequestMapping(value = AppConstants.LOGON_URL_WITH_SLASH, method = GET)
    public ModelAndView getAuthUserForm() {
        return new ModelAndView("housewifes/users/logonUserPage");
    }

    @RequestMapping(value = AppConstants.LOGON_URL_WITH_SLASH, method = POST)
    public Object submitAuthUserForm(String userName, String userPass, HttpServletRequest request) {
        UserForm user = hiber.execute(new LogonUser(userName, userPass));
        if (user == null) {
            return "redirect:" + AppConstants.LOGON_URL_WITH_SLASH;
        }
        request.getSession().setAttribute(AppConstants.SESSION_USER, user);
        return "redirect:/main.html";
    }

    @RequestMapping(value = "/logoff.html")
    public Object submitLogoff(HttpServletRequest request) {
        UserForm user = (UserForm) request.getSession().getAttribute(AppConstants.SESSION_USER);
        if (user != null) {
            request.getSession().removeAttribute(AppConstants.SESSION_USER);
            CacheManager.getInstance().onUserLogoff(user);
        }
        return "redirect:" + AppConstants.LOGON_URL_WITH_SLASH;
    }

}
